package com.boot.kafka.rest;

import com.alibaba.fastjson.JSONObject;
import com.boot.kafka.entity.KafkaMagEntity;

import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
public class KafkaController {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @GetMapping("/")
    public String inidex() {
        return  "kafka";
    }
    @RequestMapping("/send")
    public String send(String topic,String value) {
        kafkaTemplate.send(topic, value);
        return value;
    }



    @RequestMapping("/send2")
    public String send4(String value) {

        String data = "  {\"data\":{  \"cashier_id\": 1,    \"finance_no\": 1,   \"order_no\": \"123456\",  \"refund_amt\": 10,  \"remark\": \"1\",  \"store_no\": 100,  \"tenant_id\": 100 },\"entity_class\":\"com.qianjing.hmy.order.entity.OrderRefundEntity\",\"type\":\"update\"} ";



        sent("order_r2p6", data);

        return data;
    }

    private   void sent(String topic,String value) {
        String timestamp = (new Date().getTime() + "").subSequence(0, 13) + "" + (((Math.random() * 9 + 1) * 100000 + "").substring(0, 6));
        ProducerRecord<String, String> record = new ProducerRecord(topic, timestamp, value);
        ListenableFuture send = kafkaTemplate.send(record);
    }





}
